package org.song.util;

import cn.hutool.core.lang.Assert;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.ServiceException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.song.exception.myException.CommonException;
import org.springframework.lang.Nullable;

import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

@ApiModel( value = "阿里云操作工具类" )
@Slf4j
public class AliYunUtil {

    /**
     * 阿里云公共属性
     */
    @ApiModelProperty( value = "阿里云accessKeyId" , notes = "阿里云accessKeyId" )
    public static final String accessKeyId = "LTAI4G3TthHRAt3d8xh88Jrt";

    @ApiModelProperty( value = "阿里云accessKeySecret" , notes = "阿里云accessKeySecret" )
    public static final String accessKeySecret = "C4Pic7dj9nP35om7xyITiNV42xRi6w";


    /**
     * 阿里云OSS属性
     */
    @ApiModelProperty( value = "ossBucketName" , notes = "ossBucketName" )
    public static final String ossBucketName = "wsong";

    @ApiModelProperty( value = "ossEndpoint" , notes = "ossEndpoint" )
    public static final String ossEndpoint = "oss-cn-hangzhou.aliyuncs.com";


    /**
     * 阿里云短信发送属性
     */
    @ApiModelProperty( value = "阿里云短信smsRegionId" , notes = "阿里云短信smsRegionId" )
    public static final String smsRegionId = "";

    @ApiModelProperty( value = "阿里云短信smsDomain" , notes = "阿里云短信smsDomain" )
    public static final String smsDomain = "";

    @ApiModelProperty( value = "阿里云短信smsProduct" , notes = "阿里云短信smsProduct" )
    public static final String smsProduct = "";

    @ApiModelProperty( value = "阿里云短信smsTemplateCode" , notes = "阿里云短信smsTemplateCode" )
    public static final String smsTemplateCode = "";

    @ApiModelProperty( value = "阿里云smsSignName" , notes = "阿里云smsSignName" )
    public static final String smsSignName = "";



    /**
     * 上传文件至OSS返回临时url链接
     * @param objectName 保存的实例名 (文件名)
     * @param inputStream 文件流
     * @return 文件的访问路径 有效时间为一个小时
     */
    public static void putObjectToOss(String objectName, InputStream inputStream) {
        OSS ossClient = null;
        try {
            // 创建OSS客户端工具实例
            ossClient = new OSSClientBuilder().build(ossEndpoint, accessKeyId, accessKeySecret);
            // 上传文件至oss服务器
            ossClient.putObject(ossBucketName,  objectName , inputStream);
        } catch (Exception oe) {
            log.error(oe.getMessage(), oe);
            throw new ServiceException("OSS service error.");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    /**
     * 根据OSS对象名获取文件URL
     * @param objectName
     * @return
     */
    public static URL getInstanceFromOss(@Nullable String objectName  , @Nullable Long expire ) {
        Assert.isTrue( Objects.nonNull( objectName ) , () ->  new CommonException("获取实例名不能为空") );
        // 默认链接失效时间为1小时
        expire = Optional.ofNullable( expire ).orElse( System.currentTimeMillis() + 3600 * 1000 );
        OSS ossClient = null;
        try {
            // 创建OSS实例
            ossClient = new OSSClientBuilder().build(ossEndpoint, accessKeyId, accessKeySecret);
            // 获取1小时有效的临时链接 防止盗链
            return ossClient.generatePresignedUrl( ossBucketName, objectName, new Date( expire ) );
        } catch ( ClientException clientException ) {
            log.error( clientException.getMessage() , Objects.toString( clientException ));
            throw new CommonException( "OSS service error." );
        } finally {
            if ( Objects.nonNull( ossClient ) ) {
                ossClient.shutdown();   // 关闭资源
            }
        }
    }



}
